package com.zww.system.manager.dao;

import com.zww.system.manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];
    public boolean addTeacher(Teacher t){
        int index = -1;
        for(int i=0;i<teachers.length;i++){
            Teacher teacher = teachers[i];
            if (teacher==null){
                index = i;
                break;
            }
        }
        if(index==-1){
            return false;
        }else {
            teachers[index]=t;
            return true;
        }
    }
    public void deleteTeacherById(String id){
        // 查询id 在数组中的索引位置
        int index = getIndex(id);
        // 将该索引位置的元素，使用null进行替换
        teachers[index] = null;
    }
    private int getIndex(String id) {
        /*
        * 获取某个id在数组中对应的索引位置
        * */
        int index = -1;
        for (int i=0;i<teachers.length;i++){
            Teacher t = teachers[i];
            if(t!=null &&t.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }
    public void updateTeacher(String id,Teacher newTeacher){
        int index = getIndex(id);
        teachers[index]=newTeacher;
    }
    public Teacher[] findAllTeacher(){
        return teachers;
    }
}
